//
//  ViewController.m
//  UI04_UIControl
//
//  Created by Hanlingyu on 16/7/19.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor blackColor];
    
    
    // 一. UISwitch 开关控件
    // Switch 宽和高不影响控件大小
    UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(100, 50, 200, 50)];
    // 1. 开启时的控件内部颜色
    switchControl.onTintColor = [UIColor redColor];
    // 2. 边框及切换到关闭时的颜色(null_resettable) (用懒加载, 属性用null_resettable)
    switchControl.tintColor = [UIColor cyanColor];
    // 3. 开关按钮(圆圈)的颜色
    switchControl.thumbTintColor = [UIColor blackColor];
    // 4. 打开时图片, iOS7后失效
    switchControl.onImage = [UIImage imageNamed:@"userHeaderImage.jpg"];
    // 5. 关闭时的图片, iOS后失效
    switchControl.offImage = [UIImage imageNamed:@"userHeaderImage.jpg"];
    // 6. 判断SwitchControl的开闭状态 NO为关闭, YES为开启
    // 通过setter方法, 改变switchControl的初始状态
    switchControl.on = YES;
    NSLog(@"%@", switchControl.isOn ? @"YES" : @"NO");
    [switchControl setOn:YES animated:YES];
    
    // 7. 添加点击事件 与(button相同)
    [switchControl addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:switchControl];
    
    
    // 二. UISlider
    // 高度不影响视图的高度, 这个控件用于居中于视图中间
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 100, 414 - 60, 50)];
//    slider.backgroundColor = [UIColor grayColor];
    // 1. 当前滑块的值
    slider.value = 0.5f;
    // 2. 滑块最大值 1.0
    slider.maximumValue = 1.0f;
    // 3. 滑块最小值 0.0
    slider.minimumValue = 0.0f;
    // 以上最大和最小值可以根据需求去改变
    // 4. minimumValueImage 最小值那边显示的图片
    // 5. maximumValueImage 最大值那边显示的图片
    slider.maximumValueImage = [UIImage imageNamed:@"1.png"];
    // 6. continuous
    slider.continuous = NO;
    // 7. 绑定事件
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    
//    // 8. minimumTrackTintColor 未移动到的区域颜色
//    slider.minimumTrackTintColor = [UIColor redColor];
//    // 9. maximumTrackTintColor 已移动到的区域颜色
//    slider.maximumTrackTintColor = [UIColor greenColor];
//    // 10. 滑块颜色
//    slider.thumbTintColor = [UIColor blackColor];
    // 11. 设置进度条滑块右边的图片
    [slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right.png"] forState:UIControlStateNormal];
    // 12. 设置进度条滑块左边的图片
    [slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left.png"] forState:UIControlStateNormal];
    // 13. 设置进度条滑块的图片
    [slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb.png"] forState:UIControlStateNormal];
    
    
    [self.view addSubview:slider];
    
    // 3. UISegmentControl

    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"first", @"secendsdfsdafasdf", @"third", @"fourth"]];
    // segment不给frame, 系统会自动计算大小(推荐: 加上frame)
    NSLog(@"%@", NSStringFromCGRect(segment.frame));
    segment.frame = CGRectMake(20, 200, 335, 50);
    // 1. momentary 设置为YES, 如果点击选中没有选中的效果
    // 默认为NO
//    segment.momentary = YES;
    
    // 2.
    NSUInteger numberOfSegments = segment.numberOfSegments;
    NSLog(@"分段控制器Item的个数: %lu", numberOfSegments);
    
    segment.tag = 1001;
    [self.view addSubview:segment];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor whiteColor];
    button.frame = CGRectMake(100, 300, 50, 30);
    [button setTitle:@"点击" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    // 3. insertSegmentWithTitle:<#(nullable NSString *)#> atIndex:<#(NSUInteger)#> animated:<#(BOOL)#>
    // 4. removeSegmentAtIndex:<#(NSUInteger)#> animated:<#(BOOL)#>
    // 5. 对应下标的标题
//    [segment setTitle:@"我擦" forSegmentAtIndex:1];
    // 6. setWidth:<#(CGFloat)#> forSegmentAtIndex:<#(NSUInteger)#> width 默认为0.0
    [segment setWidth:0 forSegmentAtIndex:0];
    // 7. 设置对应下标内容的偏移
    [segment setContentOffset:CGSizeMake(0, 0) forSegmentAtIndex:0];
    // 8. enabled 能否被选中
    [segment setEnabled:NO forSegmentAtIndex:0];
    // 9. selectedSegmentIndex选中的下标
    segment.selectedSegmentIndex = 1;
    // 10. tintColor文字以及边框颜色
    segment.tintColor = [UIColor whiteColor];
    // 11. apportionsSegmentWidthsByContent 默认为NO
    // 如果分段控制器的段宽为0, 并且该属性为YES, 系统会自动计算
    segment.apportionsSegmentWidthsByContent = YES;
    // 12. segment绑定事件
    [segment addTarget:self action:@selector(changeBackGroundColor:) forControlEvents:UIControlEventValueChanged];
    
    
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 400, 200, 50)];
    // 1. 分页控制器的总页数, 默认为0
    pageControl.numberOfPages = 5;
    // 2. 当前页
    pageControl.currentPage = 0;
    // 3. 当只有一页时, 隐藏
    pageControl.hidesForSinglePage = YES;//???
    // 4. 获取到点的大小
    CGSize size = [pageControl sizeForNumberOfPages:1];
    NSLog(@"%@", NSStringFromCGSize(size));
    pageControl.backgroundColor = [UIColor whiteColor];
    // 5. 未选中页的默认颜色
    pageControl.pageIndicatorTintColor = [UIColor redColor];
    // 6. 当前页的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    
    // 7. 绑定事件
    [pageControl addTarget:self action:@selector(pageControlChange:) forControlEvents:UIControlEventValueChanged];
    // 8. 设置YES, 关闭系统事件page的切换, 直到调用updateCurrentPageDisplay方法, 才会显示
    pageControl.defersCurrentPageDisplay = YES;
    
    [self.view addSubview:pageControl];
    
    
    
    
    
}

- (void)pageControlChange:(UIPageControl *)pagecontrol {

    NSLog(@"change");
    [pagecontrol updateCurrentPageDisplay];

}

- (void)changeBackGroundColor:(UISegmentedControl *)segment {

    switch (segment.selectedSegmentIndex) {
        case 0:
        {
            self.view.backgroundColor = [UIColor redColor];
        }
            break;
        case 1:
        {
            self.view.backgroundColor = [UIColor yellowColor];
        }
            break;
        case 2:
        {
            self.view.backgroundColor = [UIColor cyanColor];
        }
            break;
        case 3:
        {
            self.view.backgroundColor = [UIColor greenColor];
        }
            break;
            
        default:
            break;
    }
}


- (void)click:(UIButton *)button {

    UISegmentedControl *segment = [self.view viewWithTag:1001];
    [segment insertSegmentWithTitle:@"哈哈" atIndex:0 animated:YES];

//    [segment removeSegmentAtIndex:0 animated:YES];

}

- (void)sliderAction:(UISlider *)slider {

    NSLog(@"%f", slider.value);
    if (slider.value == 1) {
        
        slider.maximumValueImage = [UIImage imageNamed:@"2.png"];

    } else {
        slider.maximumValueImage = [UIImage imageNamed:@"1.png"];
    }
    
    

}


// switch触发的事件
- (void)switchAction:(UISwitch *)switchControl {
    // 根据switch的状态, 决定触发什么样的事件
    if (switchControl.on == YES) {
        NSLog(@"开");
    } else {
        NSLog(@"关");
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
